package com.accesshollywood.grfxOrdering.Dialogs;

import java.awt.Dimension;
import java.awt.Window;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import org.jdesktop.swingx.VerticalLayout;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.User;

public class CommDialog extends AbstractCustomDialog {
	public static JTextArea	commandArea;
	public static JList		connectionList;

	public CommDialog(Window w, ModalityType modality) {
		super(w, modality);
		createDialog();
	}

	public void createDialog() {
		this.setTitle(Users.currentUser.getIP_local() + "  " + Users.currentUser.getFullName());
		setBounds(100, 100, 450, 450);
		setLayout(new VerticalLayout());

		JPanel commandPanel = new JPanel();
		commandPanel.setBorder(new TitledBorder("COMMANDS"));
		add(commandPanel);

		commandArea = new JTextArea(10, 50);
		JScrollPane scrollpane = new JScrollPane(commandArea);
		commandPanel.add(scrollpane);

		JPanel connectionPanel = new JPanel();
		connectionPanel.setBorder(new TitledBorder("CONNECTIONS"));
		add(connectionPanel);

		connectionList = new JList();
		connectionList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		connectionList.setVisibleRowCount(-1);
		JScrollPane scrollpane2 = new JScrollPane(connectionList);
		scrollpane2.setPreferredSize(new Dimension(250, 80));
		connectionPanel.add(scrollpane2);

		setVisible(true);

		GFXSQL.refreshConnections();
	}

	public static void addCommand(String str) {
		if (commandArea != null) {
			commandArea.setText(commandArea.getText() + "\n" + str);
		}
	}

	public static void refreshConnectionList() {
		if (connectionList != null) {
			// GFXSQL.refreshConnections();
			DefaultListModel model = new DefaultListModel();
			for (User currUser : GFXSQL.connectedUsers) {
				model.addElement(currUser.getIP() + " -- " + currUser.getFullName());
			}
			connectionList.setModel(model);
		}
	}
}
